package unit4.part12.ticTacToe;

public class Competition {
    private final Player[] players;
    private boolean nextFirstOrSecond = true;
    private int maxRoundNum;

    public Competition(String firstName, String secondName, int n) {
        this.players = new Player[]{new Player(firstName, n), new Player(secondName, n)};
        this.maxRoundNum = n * n;
    }

    public void run(int i, int j) {
        Player player = nextFirstOrSecond ? players[0] : players[1];
        player.addPosition(i, j);
        nextFirstOrSecond = !nextFirstOrSecond;
    }

    public void result() {
        Player player = nextFirstOrSecond ? players[1] : players[0];
        if (player.isSuccess()) {
            System.out.println(player.getName() + " WIN");
        } else if (players[0].getNum() + players[1].getNum() >= maxRoundNum) {
            System.out.println("DRAW");
        } else {
            System.out.println("PENDING");
        }
    }
}
